<?php
class Members {

	var $member_id;
	var $firstname;
	var $lastname;
	var $email;
	var $phone;
	var $newsletter;
	var $member_group_id;
	var $CI;

	/**
	 * Session Constructor
	 *
	 * The constructor runs the session routines automatically
	 * whenever the class is instantiated.
	 */

	public function __construct() {
		$this->CI =& get_instance();

		if($this->CI->session->userdata('member_id')) {
			$this->CI->db->select();
			$this->CI->db->from('Member');
			 
			$where = array(
	        	'Member_ID'			=> (int) $this->CI->session->userdata('member_id'),
	        	'IsApproved'			=> '1',
				'MarketFID'			=> $this->CI->db->escape_str(trim($this->CI->config->item('market')))
	        );
	        	 
        	$this->CI->db->where($where);
        	 
        	$query = $this->CI->db->get();
        	 
        	if($query->num_rows() > 0) {
        		$query = $query->row();

        		$this->member_id = $query->Member_ID;
        		$this->firstname = $query->FirstName;
        		$this->lastName = $query->LastName;
        		$this->email = $query->Email;
        		$this->phone = $query->Mobile;
        		$this->member_group_id = $query->MemberGroupFID;
        	} else {
        		$this->logout();
        	}
		}
	}

	public function login($email, $password, $override = false) {
		$this->CI->db->select('TOP 1 *');
		$this->CI->db->from('dbo.Member');

		$where = array(
        	'Email'				=> $this->CI->db->escape_str(trim($email)),
			'MemberGroupFID'	=> '1',
			'IsApproved'		=> '1',
			'MarketFID'			=> $this->CI->db->escape_str(trim($this->CI->config->item('market')))
        );

        	$this->CI->db->where($where);

        	$query = $this->CI->db->get();

        	if($query->num_rows() > 0) {
        		$query = $query->row();
        		$salt = $query->Salt;
        		$pwd = $query->PassWord;
        		$password = sha1($salt . sha1($salt . sha1($password)));
        		
        		if($pwd == $password) {
        			$this->CI->session->set_userdata('member_id', $query->Member_ID);

        			if ($query->ShortList && is_string($query->ShortList)) {
        				$wishList = $this->CI->session->userdata('wishlist');
        				if (!$wishList) {
        					$wishList = array();
        				}

        				$wishlist = unserialize($query->ShortList);
        				$objWishList = array();

        				foreach ($wishlist as $product_id) {
        					if (!in_array($product_id, $wishList)) {
        						array_push($objWishList, $product_id);
        					}
        					$this->CI->session->set_userdata(array("wishlist" => $objWishList));
        				}
        			}

        			$this->member_id = $query->Member_ID;
        			$this->firstname = $query->FirstName;
        			$this->lastName = $query->LastName;
        			$this->email = $query->Email;
        			$this->phone = $query->Mobile;
        			$this->member_group_id = $query->MemberGroupFID;

        			$data = array(
	               		'IpAddress' => $this->CI->input->server('REMOTE_ADDR')
        			);

        			$this->CI->db->where('Member_ID', (int) $this->member_id);
        			$this->CI->db->update('Member', $data);

        			return true;
        		}
        		 
        	} else {
        		return false;
        	}
	}

	public function logout() {
		$this->CI->session->unset_userdata('member_id');

		$this->member_id = '';
		$this->firstname = '';
		$this->lastname = '';
		$this->email = '';
		$this->telephone = '';
		$this->member_group_id = '';
	}

	public function isLogged() {
		return $this->member_id;
	}
	 
	public function getId() {
		return $this->member_id;
	}
	 
	public function getFirstName() {
		return $this->firstname;
	}
	
	public function getEmail() {
		return $this->email;
	}

}
?>